package com.shuzi.MediaShare.gui.setting;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shuzi.MediaShare.Constant;
import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.base.BaseFrameAtivity;
import com.shuzi.MediaShare.net.Protocol;
import com.shuzi.MediaShare.utils.Logger;

/**
 * 充值设置界面
 * 
 * @author lung 下午9:45:07 2013
 */
public class SettingActivity extends BaseFrameAtivity implements
		OnClickListener
{

	private TextView uidtTextView = null;

	private Button five = null;

	private Button ten = null;

	private Button twenty = null;

	private Button fivety = null;

	private Button hundred = null;

	private String nowCharge = "";

	private EditText editText;

	private Button nextButton;

	private ProgressDialog progressDialog;

	private ImageView backImageView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		titileBarLayout.setVisibility(View.GONE);
		uidtTextView = (TextView) findViewById(R.id.uid_name);
		uidtTextView.setText("当前账号：" + Constant.UID);
		five = (Button) findViewById(R.id.five_button);
		ten = (Button) findViewById(R.id.ten_button);
		twenty = (Button) findViewById(R.id.twenty_button);
		fivety = (Button) findViewById(R.id.fivety_button);
		hundred = (Button) findViewById(R.id.hundred_button);
		editText = (EditText) findViewById(R.id.other_edittext);
		nextButton = (Button) findViewById(R.id.next);
		backImageView = (ImageView) findViewById(R.id.back_button);

		five.setOnClickListener(this);
		ten.setOnClickListener(this);
		twenty.setOnClickListener(this);
		fivety.setOnClickListener(this);
		hundred.setOnClickListener(this);

		backImageView.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		nextButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (!nowCharge.equals(""))
				{
					printPassword();
				}else {
					Toast.makeText(SettingActivity.this, "请选择充值金额！", Toast.LENGTH_LONG)
					.show();
				}
				
			}
		});

		editText.addTextChangedListener(new TextWatcher()
		{

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count)
			{

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s)
			{
				Logger.getLogger().v("lung", s.toString());
				if (!s.toString().equals(""))
				{
					Button last = (Button) getWindow().getDecorView()
							.findViewWithTag(nowCharge);
					if (last != null)
					{
						last.setSelected(false);
						last.setBackgroundColor(Color.WHITE);
					}
					nowCharge = s.toString();
				}
			}
		});

		five.setTag("5");
		ten.setTag("10");
		twenty.setTag("20");
		fivety.setTag("50");
		hundred.setTag("100");

		loadData(0);
	}

	@Override
	protected boolean initData(Integer... params)
	{
		boolean isSuccess = true;
		if (params[0] == 1)
		{
			isSuccess = Protocol.chargeToPhone(nowCharge);
		}

		return isSuccess;
	}

	@Override
	protected void refreshView(boolean isSuccessful, Integer... params)
	{
		if (params[0] == 1)
		{
			progressDialog.dismiss();
			if (isSuccessful)
			{
				Toast.makeText(SettingActivity.this, "充值成功！", Toast.LENGTH_LONG)
						.show();
				finish();
			} else
			{
				Toast.makeText(SettingActivity.this, "充值失败！", Toast.LENGTH_LONG)
						.show();
			}
		}
	}

	@Override
	protected void tryAgain()
	{

	}

	@Override
	protected View setCenterView()
	{
		return View.inflate(this, R.layout.setting_main, null);
	}

	@Override
	public void onClick(View v)
	{
		editText.setText("");
		Button last = (Button) getWindow().getDecorView().findViewWithTag(
				nowCharge);
		if (last != null)
		{
			last.setSelected(false);
			last.setBackgroundColor(Color.WHITE);
		}
		Button nowButton = (Button) v;
		nowButton.setSelected(true);
		nowButton.setBackgroundColor(Color.RED);
		nowCharge = (String) v.getTag();
		// Toast.makeText(this, "你选择了" + nowCharge + "元", Toast.LENGTH_SHORT)
		// .show();
	}

	/**
	 * 输入密码
	 */
	public void printPassword()
	{
		final EditText editText2 = new EditText(this);
		new AlertDialog.Builder(this).setTitle("请输入密码")
				.setIcon(android.R.drawable.ic_dialog_info).setView(editText2)
				.setPositiveButton("确定", new DialogInterface.OnClickListener()
				{

					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						if (editText2.getText().toString().equals("9008"))
						{
							showDialog();
							loadData(1);
						} else
						{
							dialog.dismiss();
							Toast.makeText(SettingActivity.this, "密码不正确",
									Toast.LENGTH_SHORT).show();
						}

					}
				}).setNegativeButton("取消", null).show();
	}

	/**
	 * 显示弹出框
	 */
	public void showDialog()
	{
		progressDialog = ProgressDialog.show(this, "温馨提示...", "正在充值...", true,
				false);
	}

}
